<?php
/**
 * Description: 获取最近的公开课信息
 * User: devforma
 * Date: 15/5/11
 * Time: 20:33
 */
require LIB_DIR . '/LiveCourse.class.php';

class OpenCourseDetail implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $userId = $queryArgs['user_id'];

        if (empty($queryArgs['course_id']) || !is_numeric($queryArgs['course_id']))
            return genOutput(1000);

        $course = LiveCourse::getCourseDetail($cmsdb, $queryArgs['course_id']);

        if (empty($course))
            return genOutput(1000);

        $sql = 'SELECT t.name, c.start_time, c.end_time FROM `data_teacher` AS t INNER JOIN `data_live_class` AS c ON c.lector_id=t.id
        INNER JOIN `relationship_live_class_and_course` AS r ON c.id=r.class_id AND r.course_id=:course_id';
        $info = $cmsdb->getRow($sql, array(':course_id' => $queryArgs['course_id']));

        return genOutput(1, array(
            'course' => array(
                'cover_pic' => empty($course->cover_pic) ? '' : getSecureUrl($course->cover_pic),
                'name' => $course->course_name,
                'start_time' => empty($info->start_time) ? '' : $info->start_time,
                'end_time' => empty($info->end_time) ? '' : $info->end_time,
                'lector' => empty($info->name) ? '' : $info->name
            ),
            'booked' => LiveCourse::isOpenCourseBooked($cmsdb, $userId, $queryArgs['course_id'])
        ));
    }
}